package com.example.mall.web.controller;

import com.example.mall.domain.CouponUsage;
import com.example.mall.dto.CouponUsageDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.CouponUsageQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ICouponUsageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/couponUsage")
public class CouponUsageController {

    @Autowired
    public ICouponUsageService couponUsageService;

    /**
     * 分页查询优惠券使用记录表信息
     * @param couponUsageQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询优惠券使用记录表信息",notes = "分页查询优惠券使用记录表信息",httpMethod = "GET")
    public JSONResult queryPage(CouponUsageQuery couponUsageQuery){
        PageDTO<CouponUsageDto> couponUsageDtoPageDTO = couponUsageService.queryPage(couponUsageQuery);
        return JSONResult.success("查询成功",couponUsageDtoPageDTO);
    }

    /**
     * 根据id查询优惠券使用记录表信息
     * @param usageId
     * @return
     */
    @GetMapping("query/{usageId}")
    @ApiOperation(value = "根据id查询优惠券使用记录表信息",notes = "根据id查询优惠券使用记录表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("usageId")Long usageId){
        //1.根据id查询用户信息
        if (usageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USAGEID_NOT_ID);
        }
        CouponUsage couponUsage = couponUsageService.getById(usageId);
        return JSONResult.success("查询成功",couponUsage);
    }

    /**
     * 根据id查询优惠券使用记录表信息
     * @param usageId
     * @return
     */
    @GetMapping("queryCouponUsageById/{usageId}")
    @ApiOperation(value = "根据id查询优惠券使用记录表信息",notes = "根据id查询优惠券使用记录表信息",httpMethod = "GET")
    public JSONResult queryCouponUsageById(@PathVariable("id")Long usageId){
        //1.根据id查询用户信息
        if (usageId == null){
            throw new GlobalCustomerException(ErrorEnumCode.USAGEID_NOT_ID);
        }
        CouponUsage couponUsage = couponUsageService.queryCouponUsageById(usageId);
        return JSONResult.success("查询成功",couponUsage);
    }

    /**
     * 查询全部优惠券使用记录表信息
     * @param
     * @return
     */
    @GetMapping("queryCouponUsageList")
    @ApiOperation(value = "查询全部优惠券使用记录表信息",notes = "查询全部优惠券使用记录表信息",httpMethod = "GET")
    public JSONResult queryCouponUsageList(){
        List<CouponUsage> couponUsages = couponUsageService.queryCouponUsageList();
        return JSONResult.success("查询成功",couponUsages);
    }

    /**
     * 新增优惠券使用记录表
     * @param couponUsageDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增优惠券使用记录表",notes = "新增优惠券使用记录表",httpMethod = "POST")
    public JSONResult insert(@RequestBody CouponUsageDto couponUsageDto){
        couponUsageService.insert(couponUsageDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改优惠券使用记录表信息
     * @param couponUsageDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改优惠券使用记录表信息",notes = "修改优惠券使用记录表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody CouponUsageDto couponUsageDto){
        couponUsageService.update(couponUsageDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除优惠券使用记录表信息
     * @param usageId
     * @return
     */
    @PostMapping("delete/{usageId}")
    @ApiOperation(value = "删除优惠券使用记录表信息",notes = "删除优惠券使用记录表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("usageId") Long usageId) {
        couponUsageService.delete(usageId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除优惠券使用记录表信息
     * @param usageIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除优惠券使用记录表信息",notes = "批量删除优惠券使用记录表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> usageIds) {
        couponUsageService.deleteBatch(usageIds);
        return JSONResult.success("批量删除成功");
    }
}
